<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <style>

        /* ----------------------------------------------------------------------------------------------------------*/
        /* NavCubePlugin */
        /* ----------------------------------------------------------------------------------------------------------*/

        #myNavCubeCanvas {
            position: absolute;
            width: 250px;
            height: 250px;
            bottom: 50px;
            right: 10px;
            z-index: 200000;
        }

        /* ----------------------------------------------------------------------------------------------------------*/
        /* TreeViewPlugin */
        /* ----------------------------------------------------------------------------------------------------------*/

        #treeViewContainer {
            pointer-events: all;
            height: 100%;
            overflow-y: scroll;
            overflow-x: hidden;
            position: absolute;
            background-color: rgba(255, 255, 255, 0.2);
            color: black;
            top: 80px;
            z-index: 200000;
            float: left;
            left: 0;
            padding-left: 10px;
            font-family: 'Roboto', sans-serif;
            font-size: 15px;
            user-select: none;
            -ms-user-select: none;
            -moz-user-select: none;
            -webkit-user-select: none;
            width: 350px;
        }

        #treeViewContainer ul {
            list-style: none;
            padding-left: 1.75em;
            pointer-events: none;
        }

        #treeViewContainer ul li {
            position: relative;
            width: 500px;
            pointer-events: none;
            padding-top: 3px;
            padding-bottom: 3px;
            vertical-align: middle;
        }

        #treeViewContainer ul li a {
            background-color: #eee;
            border-radius: 50%;
            color: #000;
            display: inline-block;
            height: 1.5em;
            left: -1.5em;
            position: absolute;
            text-align: center;
            text-decoration: none;
            width: 1.5em;
            pointer-events: all;
        }

        #treeViewContainer ul li a.plus {
            background-color: #ded;
            pointer-events: all;
        }

        #treeViewContainer ul li a.minus {
            background-color: #eee;
            pointer-events: all;
        }

        #treeViewContainer ul li a:active {
            top: 1px;
            pointer-events: all;
        }

        #treeViewContainer ul li span:hover {
            color: white;
            cursor: pointer;
            background: black;
            padding-left: 2px;
            pointer-events: all;
        }

        #treeViewContainer ul li span {
            display: inline-block;
            width: calc(100% - 50px);
            padding-left: 2px;
            pointer-events: all;
            height: 23px;
        }

        #treeViewContainer .highlighted-node { /* Appearance of node highlighted with TreeViewPlugin#showNode() */
            border: black solid 1px;
            background: yellow;
            color: black;
            padding-left: 1px;
            padding-right: 5px;
            pointer-events: all;
        }

    </style>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<canvas id="myNavCubeCanvas"></canvas>
<div id="treeViewContainer"></div>
<div class="slideout-sidebar">
    <img class="info-icon" style="width:280px; padding-bottom:10px;" src="../../assets/images/bim_icon.png"/>
    <h1>XKTLoaderPlugin and Data Textures</h1>
    <h2>Loading a BIM model from an XKT file into a data texture model representation</h2>
    <h3>Stats</h3>
    <ul>
        <li>
            <div id="time">Loading JavaScript modules...</div>
        </li>
    </ul>
    <h3>Components used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/TreeViewPlugin/TreeViewPlugin.js~TreeViewPlugin.html"
               target="_other">TreeViewPlugin</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/NavCubePlugin/NavCubePlugin.js~NavCubePlugin.html"
               target="_other">NavCubePlugin</a>
        </li>
    </ul>
    <h3>Tutorials</h3>
    <ul>
        <li>
            <a href="https://www.notion.so/xeokit/Compact-Model-Representation-using-Data-Textures-e8093ae372fa47bf9995c26dc24ccd53?pvs=4"
               target="_other">Compact Model Representation using Data Textures</a>
        </li>
    </ul>
    <h3>Assets</h3>
    <ul>
        <li>
            <a href="http://openifcmodel.cs.auckland.ac.nz/Model/Details/274"
               target="_other">Model source</a>
        </li>
    </ul>
</div>
</body>

<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, XKTLoaderPlugin, TreeViewPlugin, NavCubePlugin, FastNavPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer, arrange the camera, tweak x-ray and highlight materials
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        transparent: true,
        saoEnabled: true,
        dtxEnabled: true
    });

    viewer.camera.eye = [110.27, 172.88, -6.49];
    viewer.camera.look = [33.88, 177.99, -101.79];
    viewer.camera.up = [0.02, 0.99, 0.03];

    new FastNavPlugin(viewer, {
        hideEdges: true,
        hideSAO: true,
        hideColorTexture: true,
        hidePBR: true,
        hideTransparentObjects: false,
        scaleCanvasResolution: false,
        scaleCanvasResolutionFactor: 0.5,
        delayBeforeRestore: true,
        delayBeforeRestoreSeconds: 0.4
    });

    //------------------------------------------------------------------------------------------------------------------
    // Create an IFC structure tree view, configured to automatically add each model as it's loaded
    //------------------------------------------------------------------------------------------------------------------

    const treeView = new TreeViewPlugin(viewer, {
        containerElement: document.getElementById("treeViewContainer"),
        autoExpandDepth: 0 // Don't initially expand nodes (default)
    });

    new NavCubePlugin(viewer, {
        canvasId: "myNavCubeCanvas",
        visible: true,
        size: 250,
        alignment: "bottomRight",
        bottomMargin: 100,
        rightMargin: 10
    });

    //----------------------------------------------------------------------------------------------------------------------
    // Load seven models and fit it to view
    //----------------------------------------------------------------------------------------------------------------------

    const xktLoader = new XKTLoaderPlugin(viewer);

    var t0 = performance.now();

    document.getElementById("time").innerHTML = "Loading model 1 of 7";

    const mechanical = xktLoader.load({
        id: "mechanical",
        src: "../../assets/models/xkt/v2/WestRiverSideHospital/mechanical.xkt",
        metaModelSrc: "../../assets/models/xkt/v2/WestRiverSideHospital/mechanical.json",
        edges: true
    });

    mechanical.on("loaded", () => {

        document.getElementById("time").innerHTML = "Loading model 2 of 7";

        const plumbing = xktLoader.load({
            id: "plumbing",
            src: "../../assets/models/xkt/v2/WestRiverSideHospital/plumbing.xkt",
            metaModelSrc: "../../assets/models/xkt/v2/WestRiverSideHospital/plumbing.json",
            edges: true
        });

        plumbing.on("loaded", () => {

            document.getElementById("time").innerHTML = "Loading model 3 of 7";

            const electrical = xktLoader.load({
                id: "electrical",
                src: "../../assets/models/xkt/v2/WestRiverSideHospital/electrical.xkt",
                metaModelSrc: "../../assets/models/xkt/v2/WestRiverSideHospital/electrical.json",
                edges: true
            });

            electrical.on("loaded", () => {

                document.getElementById("time").innerHTML = "Loading model 4 of 7";

                const fireAlarms = xktLoader.load({
                    id: "fireAlarms",
                    src: "../../assets/models/xkt/v2/WestRiverSideHospital/fireAlarms.xkt",
                    metaModelSrc: "../../assets/models/xkt/v2/WestRiverSideHospital/fireAlarms.json",
                    edges: true
                });

                fireAlarms.on("loaded", () => {

                    document.getElementById("time").innerHTML = "Loading model 5 of 7";

                    const sprinklers = xktLoader.load({
                        id: "sprinklers",
                        src: "../../assets/models/xkt/v2/WestRiverSideHospital/sprinklers.xkt",
                        metaModelSrc: "../../assets/models/xkt/v2/WestRiverSideHospital/sprinklers.json",
                        edges: true
                    });

                    sprinklers.on("loaded", () => {

                        document.getElementById("time").innerHTML = "Loading model 6 of 7";

                        const structure = xktLoader.load({
                            id: "structure",
                            src: "../../assets/models/xkt/v2/WestRiverSideHospital/structure.xkt",
                            metaModelSrc: "../../assets/models/xkt/v2/WestRiverSideHospital/structure.json",
                            excludeUnclassifiedObjects: true,
                            edges: true
                        });

                        structure.on("loaded", () => {

                            document.getElementById("time").innerHTML = "Loading model 7 of 7";

                            const architectural = xktLoader.load({
                                id: "architectural",
                                src: "../../assets/models/xkt/v2/WestRiverSideHospital/architectural.xkt",
                                metaModelSrc: "../../assets/models/xkt/v2/WestRiverSideHospital/architectural.json",
                                excludeUnclassifiedObjects: true,
                                edges: true
                            });

                            architectural.on("loaded", () => {

                                document.getElementById("time").innerHTML = "Loaded model 7 of 7";

                                var numEntities = 0;

                                for (var id in viewer.scene.models) {
                                    const sceneModel = viewer.scene.models[id];
                                    numEntities += sceneModel.numEntities;
                                }

                                var t1 = performance.now();

                                document.getElementById("time").innerHTML = "Loaded 7 models in " + Math.floor((t1 - t0)) / 1000 + " seconds<br>Objects: " + numEntities;
                            });
                        });
                    });
                });
            });
        });
    });


</script>
</html>
